package no.rodland.acc.converters;

import org.apache.struts2.util.StrutsTypeConverter;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @Since: Jun 24, 2010 10:33:15 AM
 * @Author: fmr
 */
public class DateConverter extends StrutsTypeConverter {

    private DateTimeFormatter fmt;

    public DateConverter() {
        this(DateTimeFormat.forPattern("yyyyMMdd"));
    }

    public DateConverter(DateTimeFormatter fmt) {
        this.fmt = fmt;
    }

    @Override
    public Object convertFromString(Map context, String[] values, Class toClass) {
        return fmt.parseDateTime(values[0]);
    }

    @Override
    public String convertToString(Map context, Object o) {
        return fmt.print((DateTime) o);
    }
}
